import {
  defineStore
} from 'pinia'

export const userStore = defineStore('user', () => {
  const supplierName = localStorage.getItem('_third_login_supplierName')
  const accAliasName = localStorage.getItem('_third_login_accAliasName')
  const isSuperAdmin = localStorage.getItem('_third_superAdmin')
  const userId = localStorage.getItem('_third_login_userId')
  const account = localStorage.getItem('_third_login_account')
  const token = localStorage.getItem('_third_token')

  const login = (supplier) => {
    if(localStorage.getItem('_third_login_supplierName')){
      localStorage.removeItem('_third_login_supplierName')
    }
    localStorage.setItem('_third_login_account', supplier.account)
    localStorage.setItem('_third_login_supplierName', supplier.supplierName)
    localStorage.setItem('_third_login_accAliasName', supplier.accAliasName)
    localStorage.setItem('_third_login_userId', supplier.supplierId)
    localStorage.setItem('_third_token', supplier.token)
    localStorage.setItem('_third_superAdmin', supplier.superAdmin)
  }


  const logout = () => {
    localStorage.removeItem('_third_token')
  }

  return {
    supplierName,
    accAliasName,
    isSuperAdmin,
    userId,
    account,
    token,
    login,
    logout
  }
})
